package com.zy.dao.impl;

import com.zy.dao.GetShopDao;
import com.zy.entity.Shop;
import com.zy.utils.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GetShopDaoImpl implements GetShopDao {

    @Override
    public List<Shop> getAllShops() {
        List<Shop> shops = new ArrayList<>();
        String sql = "SELECT * FROM tb_shops";
        try (Connection connection = JdbcUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Shop shop = new Shop();
                shop.setId(resultSet.getInt("id"));
                shop.setName(resultSet.getString("name"));
                shop.setUrl(resultSet.getString("image_url"));
                shop.setPrice(resultSet.getDouble("price"));
                shop.setContactPerson(resultSet.getString("contact_person"));
                shop.setContactPhone(resultSet.getString("contact_number"));
                shops.add(shop);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return shops;
    }
}
